﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using DataAccess.Entities;
using DataAccess.Repositories;
using Zeenck.Extensions;

namespace Zeenck.DataAccess
{
    public static class AccountModule
    {
        #region Create

        public static Account CreateAccount(String firstName, String lastName, String username, String email, String password, UrlHelper helper)
        {
            Account account;

            try
            {
                var profile = new Profile { Complete = false, Birthday = DateTime.Now };
                account = new Account
                {
                    FirstName = firstName,
                    LastName = lastName,
                    UserName = username,
                    Email = email,
                    Password = password,
                    Profile = profile
                };

                IRepository<Profile> profileRepo = new ProfileRepository();
                profileRepo.Save(profile);
                IRepository<Account> repo = new AccountRepository();
                repo.Save(account);
                profile.Account = account;
                profileRepo.Update(profile);


                SendAccountConfirmationEmail(firstName, lastName, username, email, password, helper);
            } catch
            {
                account = default(Account);
            }

            return account;
        }

        public static void SendAccountConfirmationEmail(String firstName, String lastName, String username, String emailAddress, String password, UrlHelper helper)
        {
            try
            {
                const string confirmationCode = "xxx-yyy-zzz";
                var confirmationLink = helper.RegistrationConfirmation(confirmationCode);
                var siteRoot = new Uri(helper.Root(true));
                EmailModule.SendAccountConfirmationEmail(firstName, lastName, emailAddress, confirmationLink, siteRoot);
            }
            catch (Exception)
            {
            }
        }

        #endregion Create

        #region Read

        public static Account ReadProfile(String emailAddress)
        {
            return default(Account);
        }

        #endregion Read

        #region Reset Password

        public static Boolean IsValidConfirmationCode(String confirmationCode)
        {
            Boolean isValid;

            try
            {
                String emailAddress = confirmationCode.UrlTokenDecode();
                isValid = true;
                // TODO
            }
            catch (Exception)
            {
                isValid = false;
            }

            return isValid;
        }

        public static void ResetPassword(String emailAddress, UrlHelper helper)
        {
            try
            {
                SendAccountPasswordResetEmail(emailAddress, helper);
            }
            catch (Exception)
            {
            }
        }

        private static void SendAccountPasswordResetEmail(String emailAddress, UrlHelper helper)
        {
            try
            {
                IRepository<Account> repo = new AccountRepository();
                foreach (var account in repo.GetAll())
                {
                    if(account.Email.Equals(emailAddress))
                    {
                        String confirmationCode = emailAddress.UrlTokenEncode();
                        String confirmationLink = helper.ResetPasswordConfirmation(confirmationCode);
                        var siteRoot = new Uri(helper.Root(true));
                        confirmationLink = siteRoot + confirmationLink;
                        EmailModule.SendAccountPasswordResetEmail(account.FirstName, account.LastName, emailAddress, confirmationLink, siteRoot);
                        break;
                    }
                }

                
            }
            catch (Exception)
            {
            }
        }

        #endregion ResetPassword
    }
}